package com.bridgeintelligent.tag.webserver.security;

import com.bridgeintelligent.tag.constants.Permissions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class PermissionDefinition extends HashMap<String, String> {
    public PermissionDefinition() {
        for (Permissions permission : Permissions.values()) {
            if (StringUtils.isEmpty(permission.getUrl()) || StringUtils.isEmpty(permission.getCode())) {
                continue;
            }
            String key = permission.getUrl();
            String value = "tagPerms[" + permission.getCode() + "]";
            if (this.containsKey(key)) {
                StringBuilder perms = new StringBuilder(this.get(key));
                perms.deleteCharAt(perms.length() - 1);
                value = perms.append(",").append(permission.getCode()).append("]").toString();
            }
            this.put(key, value);
        }
    }
}
